package com.yxhl.km.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yxhl.km.R;
import com.yxhl.km.module.entity.EventBean;
import com.yxhl.km.module.login.entity.UserInfoBean;
import com.yxhl.km.module.utils.Constants;
import com.yxhl.km.module.utils.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;

/**
 * Created by 张亚楠 on 2018/5/19.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    protected Activity mContext;
    public Bundle Bundle = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        int layoutId = getLayoutId();
        if (layoutId != 0) {
            setContentView(layoutId);
        }
        ButterKnife.bind(this);
        //数据信息传递Bunder
        Bundle = getIntent().getBundleExtra("Bundle");
        /**
         * 注册所有Activity，全局管理Activity
         */
        IntentFilter infi = new IntentFilter();
        infi.addAction(Constants.LOGINOUT);
        registerReceiver(br, infi);
        //数据解析
        preliminary(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            //注册事件
            EventBus.getDefault().register(this);
        }

    }

    /**
     * 初始化控件数据解析
     *
     * @param savedInstanceState
     */

    private void preliminary(Bundle savedInstanceState) {

        // 初始化数据
        initialized(savedInstanceState);
        // 监听
        initListener();
    }

    /**
     * 监听事件
     */
    protected abstract void initListener();

    /**
     * 初始化组件
     *
     * @param savedInstanceState
     */

    protected abstract void initialized(Bundle savedInstanceState);

    /**
     * 布局文件ID
     *
     * @return
     */
    protected abstract int getLayoutId();

    protected BroadcastReceiver br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Constants.LOGINOUT)) {
                finish();
            }
        }
    };
    /**
     * 标题
     */
    protected TextView titlebar_left_text, titlebar_title, titlebar_right_text;
    protected ImageView titlebar_right_iv, titlebar_left_iv;

    protected void initTitle(String left, String title, String right) {
        titlebar_title = (TextView) findViewById(R.id.titlebar_title_text);
        titlebar_left_text = (TextView) findViewById(R.id.titlebar_left_text);
        titlebar_right_text = (TextView) findViewById(R.id.titlebar_right_text);
        titlebar_right_iv = (ImageView) findViewById(R.id.titlebar_right_iv);
        titlebar_left_iv = (ImageView) findViewById(R.id.titlebar_left_iv);
        if (null != titlebar_left_text) {
            if (TextUtils.isEmpty(left)) {
                titlebar_left_text.setVisibility(View.GONE);
            } else {
                titlebar_left_text.setText(left);
                titlebar_left_text.setVisibility(View.VISIBLE);
            }
        }
        if (null != titlebar_right_text) {
            if (TextUtils.isEmpty(right)) {
                titlebar_right_text.setVisibility(View.GONE);
            } else {
                titlebar_right_text.setText(right);
                titlebar_right_text.setVisibility(View.VISIBLE);
            }
        }

        if (null != titlebar_right_text) {
            if (TextUtils.isEmpty(title)) {
                titlebar_right_text.setVisibility(View.GONE);
            } else {
                titlebar_title.setText(title);
                titlebar_right_text.setVisibility(View.VISIBLE);
            }
        }
        titlebar_left_text.setOnClickListener(this);
        titlebar_right_text.setOnClickListener(this);
        titlebar_right_iv.setOnClickListener(this);
        titlebar_title.setOnClickListener(this);
        titlebar_left_iv.setOnClickListener(this);
    }

    public UserInfoBean getUserInfo() {
        UserInfoBean userInfoBean = (UserInfoBean) SharedPreferencesUtils.getObjectFromShare(mContext, Constants.USERINFO);
        if (userInfoBean == null) {
            return null;

        }
        return userInfoBean;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.titlebar_left_iv:
                finishAnimation();
                break;
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(EventBean eventBean) {
    }

    /**
     * 动画跳转Activity
     *
     * @param clazz
     * @param finish
     * @param bundle
     */
    public void gotoActivity(Class<? extends Activity> clazz, boolean finish, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (bundle != null) {
            intent.putExtra("Bundle", bundle);
        }
        this.startActivity(intent);
        if (finish) {
            this.finish();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            //设置切换动画，从右边进入，左边退出
            this.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
    }

    public void finishAnimation() {
        finish();
        this.overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(br);
        //取消注册事件
        EventBus.getDefault().unregister(this);
    }
}
